﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.Windows.Forms;
using System.Diagnostics;

namespace MangaCafe
{
    public class batoto
    {
        public mangasite mes;
        public batoto(string a, string b, string c)
        {
            mes = new mangasite(a, b, c);
        }
        public int mc(HtmlNode root)
        {
            int records = 0;
            HtmlNode node = root.SelectSingleNode("//select[@name='page_select']");
            foreach (HtmlNode node2 in node.SelectNodes(".//option[@value]"))
                records = records + 1;
            return records;
        }
        public void read(HtmlNode root, Form1 me)
        {
            foreach (HtmlNode node in root.SelectNodes("//tr[@class='row lang_English chapter_row']"))
            {
                HtmlNode node2 = node.SelectSingleNode(".//a");
                if (node2.Attributes["href"] != null)
                {
                    rd record = new rd();
                    HtmlAttribute h = node2.Attributes["href"];
                    record.link = h.Value + "/";
                    record.name = node2.InnerText.Trim();
                    record.max_chp = 0;
                    me.add_c2(record);
                }
            }
        }
        public async Task<List<li>> searchcont(HtmlNode root, Form1 me,string base_search,int show_no)
        {
            List<li> records = new List<li>();
            foreach (HtmlNode node in root.SelectNodes("//div[@id='comic_search_results']"))
            {
                if (root.SelectSingleNode(".//strong") != null)
                {
                    foreach (HtmlNode node2 in node.SelectNodes(".//strong"))
                    {
                        foreach (HtmlNode node3 in node2.SelectNodes(".//a"))
                        {
                            if (node3.Attributes["href"] != null)
                            {
                                li record = new li();
                                HtmlAttribute h = node3.Attributes["href"];
                                record.link = h.Value;
                                record.name = node3.InnerText.Trim();
                                record.mn = null;
                                me.add_c1(record);
                                records.Add(record);
                            }
                        }
                    }
                }
                else
                {
                    return null;
                }
            }
            if (root.SelectSingleNode("//tr[@id='show_more_row']") != null)
            {
                show_no += 1;
                var add = mes.search_address + base_search + "&name_cond=c&p=" + show_no.ToString();
                HtmlNode roots = await me.get_html(add);
                records = records.Concat(await searchcont(roots, me, base_search,show_no)).ToList();
            }
            return records;
        }
        public info get_info(HtmlNode root)
        {
            info record = new info();
            record.rank = "----";
            HtmlNode node = root.SelectSingleNode("//div[@class='ipsBox']");
            if(node!=null)
            {
                HtmlNode imgnode = node.SelectSingleNode(".//img");
                if (imgnode!= null)
                {
                    if (imgnode.Attributes["src"] != null)
                    {
                        HtmlAttribute h = imgnode.Attributes["src"];
                        record.pic = h.Value;
                    }
                }
            }
            foreach (HtmlNode noded in root.SelectNodes("//tr[@class='']"))
            {
                HtmlNode nodes = noded.SelectSingleNode(".//td");
                switch (nodes.InnerText.Trim())
                {
                    case "Alt Names:":
                        foreach (HtmlNode node1 in noded.SelectNodes(".//td"))
                            if (node1.InnerText.Trim() != nodes.InnerText.Trim())
                                record.altname = node1.InnerText.Trim();
                        break;
                    case "Author:":
                        HtmlNode node2 = noded.SelectSingleNode(".//a");
                        record.author = node2.InnerText.Trim();
                        break;
                    case "Artist:":
                        HtmlNode node3 = noded.SelectSingleNode(".//a");
                        record.arists = node3.InnerText.Trim();
                        break;
                    case "Genres:":
                        foreach (HtmlNode node1 in noded.SelectNodes(".//td"))
                            if (node1.InnerText.Trim() != nodes.InnerText.Trim())
                                record.genre = node1.InnerText.Trim();
                        record.genre = record.genre.Replace(" ", ", ");
                        break;
                    case "Status:":
                        foreach (HtmlNode node1 in noded.SelectNodes(".//td"))
                            if (node1.InnerText.Trim() != nodes.InnerText.Trim())
                                record.status = node1.InnerText.Trim();
                        break;
                    case "Description:":
                        foreach (HtmlNode node1 in noded.SelectNodes(".//td"))
                            if (node1.InnerText.Trim() != nodes.InnerText.Trim())
                                record.sum = node1.InnerText.Trim();
                        byte[] bytes = Encoding.Default.GetBytes(record.sum);
                        record.sum = Encoding.UTF8.GetString(bytes);
                        break;
                }
            }
            return record;
        }
        public string get_pic(HtmlNode root)
        {
            string records = "";
            HtmlNode node = root.SelectSingleNode("//div[@id='full_image']");
            HtmlNode node2 = node.SelectSingleNode(".//img");
            if (node2.Attributes["src"] != null)
            {
                HtmlAttribute h = node2.Attributes["src"];
                records = h.Value;
            }
            return records;
        }
    }
}
